package cn.seczone.iast.agent;

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.commons.AdviceAdapter;

/**
 * @author XieShaoping
 */
public class MyInputAndReturnAdapter extends AdviceAdapter {

    public static final String CLASS_PATH = InjectMethod.class.getName().replace(".", "/");

    public MyInputAndReturnAdapter(MethodVisitor mv, int access, String name, String desc) {
        super(Opcodes.ASM9, mv, access, name, desc);
    }

    protected void onMethodExit(int opcode) {
        mv.visitInsn(ICONST_3);//Object[] e;
        mv.visitTypeInsn(ANEWARRAY, "java/lang/Object");//new Object[]

        mv.visitInsn(DUP);
        mv.visitInsn(ICONST_0);
        mv.visitVarInsn(ALOAD, 1);
        mv.visitInsn(AASTORE); //a

        mv.visitInsn(DUP);
        mv.visitInsn(ICONST_1);
        mv.visitVarInsn(ILOAD, 2);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
        mv.visitInsn(AASTORE);//b

        mv.visitInsn(DUP);
        mv.visitInsn(ICONST_2);
        mv.visitVarInsn(ILOAD, 3);
        mv.visitMethodInsn(INVOKESTATIC, "java/lang/Boolean", "valueOf", "(Z)Ljava/lang/Boolean;", false);
        mv.visitInsn(AASTORE);//c

        mv.visitVarInsn(ASTORE, 4);//e = new Object[]{a, b, c};

        mv.visitVarInsn(ALOAD, 4);//加载e

        mv.visitMethodInsn(INVOKESTATIC, "cn/seczone/iast/agent/InjectMethod", "afterCode", "([Ljava/lang/Object;)V", false);
    }

}